﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using System.ComponentModel;
using Microsoft.Xna.Framework;
using SynapseGaming.LightingSystem.Rendering;
using SynapseGaming.LightingSystem.Effects.Forward;
using SynapseGaming.LightingSystem.Core;
using The_LarX.Rendering;

namespace The_LarX
{
    public class SubActor
    {
        #region Material
        /// <summary>
        /// The Texture's Place
        /// </summary>
#if WINDOWS
        [Category("Material")]
        [Description("The Texture's Place")]
#endif
        public string TextureName
        {
            get { return Material.TextureName; }
            set { Material.TextureName = value; }
        }
        public Vector4 DiffuseColor
        {
            get { return Material.DiffuseColor; }
            set { Material.DiffuseColor = value; }
        }
        public Vector4 EmissiveColor
        {
            get { return Material.EmissiveColor; }
            set { Material.EmissiveColor = value; }
        }
        public Vector4 SpecularAndAmount
        {
            get { return Material.SpecularAndAmount; }
            set { Material.SpecularAndAmount = value; }
        }
        public Texture2D NormalMapTexture
        {
            get { return Material.NormalMapTexture; }
            set { Material.NormalMapTexture = value; }
        }
        public Texture2D DiffuseMapTexture
        {
            get { return Material.DiffuseMapTexture; }
            set { Material.DiffuseMapTexture = value; }
        }
        public Texture2D EmissiveMapTexture
        {
            get { return Material.EmissiveMapTexture; }
            set { Material.EmissiveMapTexture = value; }
        }
        public Texture2D SpecularColorMapTexture
        {
            get { return Material.SpecularColorMapTexture; }
            set { Material.SpecularColorMapTexture = value; }
        }
        public Texture2D ParalaxMapTexture
        {
            get { return Material.ParalaxMapTexture; }
            set { Material.ParalaxMapTexture = value; }
        }

        public string Name
        {
            get { return Object.RenderableMeshes[MeshPartIndex].Name; }
        }

        public bool DoubleSided
        {
            get { return Material.DoubleSided; }
            set { Material.DoubleSided = value; }
        }
        public TransparencyMode TransparencyMode
        {
            get { return Material.TransparencyMode; }
            set { Material.TransparencyMode = value; }
        }
        public float Transparency
        {
            get { return Material.Transparency; }
            set { Material.Transparency = value; }
        }



        #endregion

        public Material Material;
        public Effect Effect
        {
            get { return Object.RenderableMeshes[MeshPartIndex].Effect; }
            set { Object.RenderableMeshes[MeshPartIndex].Effect = value; }
        }
        public SceneObject Object;
        private int MeshPartIndex = 0;        

        public SubActor(SceneObject part, int MeshPartIndex)
        {
            this.Object = part;
            this.MeshPartIndex = MeshPartIndex;
#if !WINDOWS_PHONE
            if (!(Effect is LightingEffect))
                Effect = new LightingEffect(Engine.GraphicsDevice);
#endif
        }

        private bool remapped;
        public bool RemappedEffect
        {
            get { return remapped; }
            set
            {
                if (value)
                {
                    if (!remapped)
                    {
                        RemapMesh();
                        remapped = true;
                    }
                }
            }
        }
        
        public void RemapMesh()
        {
            if (!remapped)
            {
                Object.RenderableMeshes[MeshPartIndex].Effect = Effect.Clone();
                //Object.RenderableMeshes[MeshPartIndex].Effect = new LightingEffect(Engine.GraphicsDevice);
#if !WINDOWS_PHONE
                Material.Effect = Object.RenderableMeshes[MeshPartIndex].Effect;
#else
                Material.Effect = (BasicEffect)Object.RenderableMeshes[MeshPartIndex].Effect;
#endif
                remapped = true;
            }
        }
        public void RemapMesh(Effect fx)
        {
            Object.RenderableMeshes[MeshPartIndex].Effect = fx;
            Material.Effect = fx;
        }

        public void SerializeData(SerializeData Data)
        {
            this.Material.SerializeData(MeshPartIndex, Data);
            Data.AddData(MeshPartIndex + "RemappedMesh", this.remapped);
        }
        public void DeserializeData(bool deserializeMaterialInfo, SerializeData Data)
        {
            remapped = Data.GetData<bool>(MeshPartIndex + "RemappedMesh");
            if (remapped)
            {
                remapped = false;
                RemapMesh();
            }
            this.Material.Effect = Effect;
            if (deserializeMaterialInfo)
            {
                this.Material.DeserializeData(MeshPartIndex, Data);
            }
        }
        public void DeserializeData(SerializeData Data)
        {
            DeserializeData(true, Data);
        }
    }
}
